<?php
class GeryHouseKeepingContext extends GeryProxyContext {
	
	var $macros;
	
	/**
	 * The GeryEngine
	 *
	 * @var GeryEngine
	 */
	var $engine;
	
	public function __construct($context, $engine) {
		parent::__construct($context);
		
		$this->engine = $engine;
		$this->macros = new HashMap();
	}
	
	function registerMacro($templateClass, $macroName) {
		$methodName = MacroNamer::getMethodName($macroName);
		
		$this->macros->put($macroName,
			new MethodProxy($templateClass, $methodName));
	}
	
	function isMacroRegistered($macroName) {
		return $this->macros->containsKey($macroName);
	}
	
	function callMacro($macroName, IGeryContext $context, $parameters) {
		$macro = $this->macros->get($macroName);
		
		if ($macro == null)
			return false;
		
		return $macro->call(array($context, $parameters));
	}
	
	function getClassNameForMacro($macroName) {
		$macro = $this->macros->get($macroName);
		
		return $macro->getClassName();
	}
	
	function getInternalHouseKeepingContext() {
		return $this;
	}
	
	function getTemplate($resourceName) {
		return $this->engine->getTemplate($resourceName);
	}
	
	function getResource($resourceName) {
		return $this->engine->getResource($resourceName);
	}
	
	function getLogger() {
		return $this->engine->getLogger();
	}
}

class MacroNamer {
	
	public static function getMethodName($macroName) {
		return sprintf("__macro_%s", strtolower($macroName));
	}
}
?>